package com.example.mymusicdisplay.view;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.viewpager.widget.ViewPager;

import com.example.mymusicdisplay.R;
import com.example.mymusicdisplay.databinding.FragmentFindMusicBinding;
import com.example.mymusicdisplay.view.adapter.BannerAdapter;

import java.util.ArrayList;
import java.util.List;

public class FindMusicFragment extends Fragment {
    private static final String TAG = "FindMusicFragment";

    private FragmentFindMusicBinding mMusicBinding;

    private Context mContext;

    private int mPosition;

    public FindMusicFragment(Context context) {
        // Required empty public constructor
        mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: ");
        // Inflate the layout for this fragment
        mMusicBinding = DataBindingUtil.inflate(LayoutInflater
            .from(mContext), R.layout.fragment_find_music, container, false);
        return mMusicBinding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        List<String> mList = new ArrayList<>();
        mList.add("https://w.wallhaven.cc/full/6o/wallhaven-6opeww.jpg");
        mList.add("https://th.wallhaven.cc/small/1k/1kg271.jpg");
        mList.add("https://w.wallhaven.cc/full/e7/wallhaven-e7623w.jpg");
        mList.add("https://w.wallhaven.cc/full/6o/wallhaven-6opeww.jpg");
        mList.add("https://th.wallhaven.cc/small/1k/1kg271.jpg");
        BannerAdapter bannerAdapter = new BannerAdapter(requireContext(), mList);
        mMusicBinding.bannerView.setAdapter(bannerAdapter);
        mMusicBinding.bannerView.setCurrentItem(1, false);

        mMusicBinding.bannerView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == 0) {
                    if (mPosition == 0) {
                        mMusicBinding.bannerView.setCurrentItem(mList.size() - 1, false);
                    } else if (mPosition == mList.size() - 1) {
                        mMusicBinding.bannerView.setCurrentItem(1, false);
                    }
                }
            }
        });

        initClickListener();
    }

    private void initClickListener() {
        mMusicBinding.goRanking.setOnClickListener(v -> {
            NavController controller = Navigation.findNavController(v);
            controller.navigate(R.id.action_mainFragment_to_rankingFragment);
        });
    }
}